package cn.com.ghy.android.bean;

import java.io.InputStream;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlSerializer;

import cn.com.ghy.android.util.XMLStatic;

import android.util.Xml;

/**
 * @软件著作权:金红叶纸业集团有限公司
 * @系统名称:SFA系统
 * @Description:今天拜访---基本信息采集数据获取解析类
 * @author wuz  @date 2011-5-28 10:04:21
 * @版本历史:v1.0
 * @Copyright:Copyright (c) 2011-2012 
 */

public class DataCollectBaseInfoBeanParser {
	public static List<DataCollectBaseInfoBean> parseXML(InputStream in) throws Exception {
        XmlPullParser parser = Xml.newPullParser();
        parser.setInput(in, "UTF-8");
        int type = parser.getEventType(); 
        
        DataCollectBaseInfoBean bean = null;
        List<DataCollectBaseInfoBean> list = null;
        
        MyWayBean myWay = null;
        CustomerInfoBean customerInfo = null;
        DisplaySiteBean displaySite = null;
        
        while (type != XmlPullParser.END_DOCUMENT) { 
            switch (type) {   
            case XmlPullParser.START_DOCUMENT:   
            	list = new ArrayList<DataCollectBaseInfoBean>();   
                break;   
   
            case XmlPullParser.START_TAG:
                if ("Table".equals(parser.getName())) {
                	bean = new DataCollectBaseInfoBean();
                	myWay = new MyWayBean();
                	customerInfo = new CustomerInfoBean();
                	displaySite = new DisplaySiteBean();
                } else if (null != list && null != bean) {
                    if ("WAYID".equals(parser.getName())) {
                    	myWay.setWayid(parser.nextText());   
                    } else if("PARTNER".equals(parser.getName())) {
                    	customerInfo.setPartner(parser.nextText());
                    } else if("ISVISIT".equals(parser.getName())) {
                    	customerInfo.setIsvisit(parser.nextText());
                    } else if("FXSID".equals(parser.getName())) {
                    	customerInfo.setFxsid(parser.nextText());
                    } else if("FXSNAME".equals(parser.getName())) {
                    	customerInfo.setFxsname(parser.nextText());
                    } else if("JXSID".equals(parser.getName())) {
                    	customerInfo.setJxsid(parser.nextText());
                    } else if("JXSNAME".equals(parser.getName())) {
                    	customerInfo.setJxsname(parser.nextText());
                    } else if("CLWZ".equals(parser.getName())) {
                    	displaySite.setClwz(parser.nextText());
                    } else if("CLWZNAME".equals(parser.getName())) {
                    	displaySite.setClwzname(parser.nextText());
                    } else if("CONTENT".equals(parser.getName())) {
                    	bean.setVisitNote(parser.nextText());
                    } else if("PLAN".equals(parser.getName())) {
                    	bean.setQuesPlan(parser.nextText());
                    } else if("ZHANBI".equals(parser.getName())) {
                    	bean.setDisplayScale(parser.nextText());
                    } else if("ISPAYCUST".equals(parser.getName())) {
                    	bean.setIsPaycust(parser.nextText());
                    }
                }   
                break;   
   
            case XmlPullParser.END_TAG:   
                if ("Table".equals(parser.getName())) {
                	if (null != list && bean != null) {
                		bean.setMyWay(myWay);
                		bean.setCustomerInfo(customerInfo);
                		bean.setDisplaySite(displaySite);
                		list.add(bean);
                		myWay = null;
                		customerInfo = null;
                		displaySite = null;
                		bean = null;
                	} 
                }
                break;
            }
            type = parser.next();
        }   
        in.close();   
        return list;  
    }
	

	public static String createXML(LoginInfoBean loginInfo, DataCollectBaseInfoBean bean,
			Writer writer) throws Exception {
        XmlSerializer xmlSerializer = Xml.newSerializer();
        
        xmlSerializer.setOutput(writer);
        xmlSerializer.startDocument("UTF-8", true);
        xmlSerializer.startTag("", XMLStatic.CONTENT);
        xmlSerializer.attribute("", XMLStatic.TOTAL_RECORD, "1");
        xmlSerializer.startTag("", XMLStatic.RECORDS);
        xmlSerializer.startTag("", XMLStatic.WAYID);
        xmlSerializer.text(bean.getMyWay().getWayid());
		xmlSerializer.endTag("", XMLStatic.WAYID);
		xmlSerializer.startTag("", XMLStatic.PARTNER);
        xmlSerializer.text(bean.getCustomerInfo().getPartner());
		xmlSerializer.endTag("", XMLStatic.PARTNER);
		xmlSerializer.startTag("", XMLStatic.PERNR);
        xmlSerializer.text(loginInfo.getPernr());
		xmlSerializer.endTag("", XMLStatic.PERNR);
		xmlSerializer.startTag("", XMLStatic.VSDATE);
        xmlSerializer.text(null == bean.getVsdate() ? "" : bean.getVsdate());
		xmlSerializer.endTag("", XMLStatic.VSDATE);
		xmlSerializer.startTag("", XMLStatic.ISVISIT);
        xmlSerializer.text(bean.getCustomerInfo().getIsvisit());
		xmlSerializer.endTag("", XMLStatic.ISVISIT);
		xmlSerializer.startTag("", XMLStatic.CLWZ);
        xmlSerializer.text(bean.getDisplaySite().getClwz());
		xmlSerializer.endTag("", XMLStatic.CLWZ);
		xmlSerializer.startTag("", XMLStatic.VSCONTENT);
        xmlSerializer.text(null == bean.getVisitNote() ? "" : bean.getVisitNote());
		xmlSerializer.endTag("", XMLStatic.VSCONTENT);
		xmlSerializer.startTag("", XMLStatic.PLAN);
        xmlSerializer.text(null == bean.getQuesPlan() ? "" : bean.getQuesPlan());
		xmlSerializer.endTag("", XMLStatic.PLAN);
		xmlSerializer.startTag("", XMLStatic.ZHANBI);
        xmlSerializer.text(null == bean.getDisplayScale() ? "" : bean.getDisplayScale());
		xmlSerializer.endTag("", XMLStatic.ZHANBI);
		xmlSerializer.startTag("", XMLStatic.ISPAYCUST);
        xmlSerializer.text(bean.getIsPaycust());
		xmlSerializer.endTag("", XMLStatic.ISPAYCUST);
		xmlSerializer.endTag("", XMLStatic.RECORDS);
		
		xmlSerializer.endTag("", XMLStatic.CONTENT);
		xmlSerializer.endDocument();
		writer.flush();
		writer.close();
		return writer.toString().replace("<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>", "");
	}
}
